<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表排序</title>
    <script src="../vue.js"></script>
</head>

<body>
    <div id="root">
        <h2>人员列表</h2>
        <input type="text" placeholder="请输入名字" v-model="keyword"><br>
        <button @click="type=1">年龄升序</button>
        <button @click="type=2">年龄降序</button>
        <button @click="type=0">原顺序</button>
        <ul>
            <li v-for="(man,index) in filterMans" :key="man.id">{{man.name}}-{{man.age}}-{{man.sex}}</li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false;
        new Vue({
            el: '#root',
            data: {
                keyword: '',
                type: 0,
                mans: [
                    {
                        id: 001,
                        name: "张三丰",
                        age: "18",
                        sex: "男"
                    },
                    {
                        id: 002,
                        name: "张无忌",
                        age: "20",
                        sex: "男"
                    },
                    {
                        id: 003,
                        name: "花无缺",
                        age: "19",
                        sex: "男"
                    },
                    {
                        id: 004,
                        name: "花灵龙",
                        age: "18",
                        sex: "男"
                    },
                    {
                        id: 005,
                        name: "李小龙",
                        age: "33",
                        sex: "男"
                    },
                    {
                        id: 006,
                        name: "马冬梅",
                        age: "24",
                        sex: "女"
                    },
                    {
                        id: 007,
                        name: "周冬雨",
                        age: "21",
                        sex: "女"
                    },
                    {
                        id: '008',
                        name: "张碧城",
                        age: "26",
                        sex: "女"
                    },
                ],
            },
            computed: {
                filterMans() {
                    const arr = this.mans.filter(man => man.name.indexOf(this.keyword) !== -1)
                    this.type && arr.sort((man1, man2) => this.type === 1 ? man1.age - man2.age : man2.age - man1.age)
                    return arr
                }
            }  
        })
    </script>
</body>

</html>